package com.longtop.sp.mapdao.util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Comparator;

/**
 * 
 * 实现比较器接口，用于对String进行不区分大小写的比较 配合TreeMap实现不区分大小写的哈西表。
 * 
 * @author leexh
 */
public class ComparatorIgnoreCase implements Comparator, Serializable {
	public ComparatorIgnoreCase() {
	}

	public int compare(Object o1, Object o2) {
		if (o1 == null && o2 == null) {
			return 0;
		}
		if (o1 == null) {
			return -1;
		}
		if (o2 == null) {
			return 1;
		}

		if (o1 instanceof BigDecimal && o2 instanceof BigDecimal) {
			return ((BigDecimal) o1).compareTo((BigDecimal) o2);
		} else if (o1 instanceof Integer && o2 instanceof Integer) {
			return ((Integer) o1).compareTo((Integer) o2);
		} else {
			String s1 = o1.toString();
			String s2 = o2.toString();
			return s1.toLowerCase().compareTo(s2.toLowerCase());
		}
	}

	public boolean equals(Object obj) {
		return compare(this, obj.toString()) == 0;
	}

	private void writeObject(ObjectOutputStream oos) throws IOException {
		oos.defaultWriteObject();
	}

	private void readObject(ObjectInputStream ois)
			throws ClassNotFoundException, IOException {
		ois.defaultReadObject();
	}

}
